using Microsoft.AspNetCore.Mvc;
using Beauty.Net.Consul.Core;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Beauty.NetWebApi.Controllers
{
    /// <summary>
    /// Consul 演示控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ConsulDemoController : ControllerBase
    {
        private readonly IConsulDiscoveryService _consulDiscoveryService;
        private readonly ILogger<ConsulDemoController> _logger;

        public ConsulDemoController(IConsulDiscoveryService consulDiscoveryService, ILogger<ConsulDemoController> logger)
        {
            _consulDiscoveryService = consulDiscoveryService;
            _logger = logger;
        }

        /// <summary>
        /// 获取所有服务
        /// </summary>
        [HttpGet("services")]
        public async Task<IActionResult> GetAllServices()
        {
            var services = await _consulDiscoveryService.GetAllServicesAsync();
            _logger.LogInformation("获取所有服务成功，共{Count}个服务", services.Count);
            return Ok(new { message = "获取服务成功", count = services.Count, data = services });
        }

        /// <summary>
        /// 根据服务名称获取服务实例
        /// </summary>
        [HttpGet("service/{serviceName}")]
        public async Task<IActionResult> GetServiceInstances(string serviceName)
        {
            if (string.IsNullOrEmpty(serviceName))
            {
                return BadRequest(new { message = "服务名称不能为空" });
            }

            var instances = await _consulDiscoveryService.GetServicesAsync(serviceName);
            _logger.LogInformation("获取服务{ServiceName}的实例成功，共{Count}个实例", serviceName, instances.Count);
            return Ok(new { message = "获取服务实例成功", serviceName, count = instances.Count, data = instances });
        }

        /// <summary>
        /// 随机获取一个可用的服务实例
        /// </summary>
        [HttpGet("service/{serviceName}/random")]
        public async Task<IActionResult> GetRandomService(string serviceName)
        {
            if (string.IsNullOrEmpty(serviceName))
            {
                return BadRequest(new { message = "服务名称不能为空" });
            }

            var instance = await _consulDiscoveryService.GetRandomServiceAsync(serviceName);
            if (instance == null)
            {
                return NotFound(new { message = "未找到可用的服务实例", serviceName });
            }

            return Ok(new { message = "获取随机服务实例成功", serviceName, data = instance });
        }

        /// <summary>
        /// 检查服务健康状态
        /// </summary>
        [HttpGet("health/{serviceId}")]
        public async Task<IActionResult> CheckServiceHealth(string serviceId)
        {
            if (string.IsNullOrEmpty(serviceId))
            {
                return BadRequest(new { message = "服务ID不能为空" });
            }

            var isHealthy = await _consulDiscoveryService.IsServiceHealthyAsync(serviceId);
            _logger.LogInformation("检查服务{ServiceId}健康状态: {IsHealthy}", serviceId, isHealthy ? "健康" : "不健康");
            return Ok(new { message = "检查服务健康状态成功", serviceId, isHealthy });
        }
    }
}